using DocumentCreationSystem.Models.AgentOps;

namespace DocumentCreationSystem.Services.AgentOps
{
    /// <summary>
    /// AgentOps监控服务接口 - 提供实时监控和性能分析
    /// </summary>
    public interface IAgentOpsMonitoringService
    {
        /// <summary>
        /// 获取实时监控仪表板数据
        /// </summary>
        /// <returns>仪表板数据</returns>
        Task<MonitoringDashboard> GetDashboardDataAsync();

        /// <summary>
        /// 获取工作流性能指标
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <param name="timeRange">时间范围</param>
        /// <returns>性能指标</returns>
        Task<WorkflowPerformanceMetrics> GetWorkflowMetricsAsync(string workflowId, TimeRange timeRange);

        /// <summary>
        /// 获取系统健康状态
        /// </summary>
        /// <returns>系统健康状态</returns>
        Task<SystemHealthStatus> GetSystemHealthAsync();

        /// <summary>
        /// 获取资源使用情况
        /// </summary>
        /// <returns>资源使用情况</returns>
        Task<ResourceUsageInfo> GetResourceUsageAsync();

        /// <summary>
        /// 获取错误和异常统计
        /// </summary>
        /// <param name="timeRange">时间范围</param>
        /// <returns>错误统计</returns>
        Task<ErrorStatistics> GetErrorStatisticsAsync(TimeRange timeRange);

        /// <summary>
        /// 获取用户活动分析
        /// </summary>
        /// <param name="timeRange">时间范围</param>
        /// <returns>用户活动分析</returns>
        Task<UserActivityAnalysis> GetUserActivityAnalysisAsync(TimeRange timeRange);

        /// <summary>
        /// 获取质量趋势分析
        /// </summary>
        /// <param name="timeRange">时间范围</param>
        /// <returns>质量趋势</returns>
        Task<QualityTrendAnalysis> GetQualityTrendAsync(TimeRange timeRange);

        /// <summary>
        /// 创建自定义监控指标
        /// </summary>
        /// <param name="metric">监控指标定义</param>
        /// <returns>创建结果</returns>
        Task<bool> CreateCustomMetricAsync(CustomMetricDefinition metric);

        /// <summary>
        /// 设置监控告警
        /// </summary>
        /// <param name="alert">告警配置</param>
        /// <returns>设置结果</returns>
        Task<bool> SetupAlertAsync(AlertConfiguration alert);

        /// <summary>
        /// 获取告警历史
        /// </summary>
        /// <param name="timeRange">时间范围</param>
        /// <returns>告警历史</returns>
        Task<List<AlertHistory>> GetAlertHistoryAsync(TimeRange timeRange);

        /// <summary>
        /// 生成监控报告
        /// </summary>
        /// <param name="reportConfig">报告配置</param>
        /// <returns>监控报告</returns>
        Task<MonitoringReport> GenerateReportAsync(ReportConfiguration reportConfig);

        /// <summary>
        /// 导出监控数据
        /// </summary>
        /// <param name="exportConfig">导出配置</param>
        /// <returns>导出结果</returns>
        Task<ExportResult> ExportDataAsync(DataExportConfiguration exportConfig);
    }

    /// <summary>
    /// 监控仪表板数据
    /// </summary>
    public class MonitoringDashboard
    {
        /// <summary>
        /// 概览统计
        /// </summary>
        public OverviewStatistics Overview { get; set; } = new();

        /// <summary>
        /// 活跃工作流
        /// </summary>
        public List<ActiveWorkflowInfo> ActiveWorkflows { get; set; } = new();

        /// <summary>
        /// 性能指标
        /// </summary>
        public PerformanceMetrics Performance { get; set; } = new();

        /// <summary>
        /// 系统状态
        /// </summary>
        public SystemStatus SystemStatus { get; set; } = new();

        /// <summary>
        /// 最近活动
        /// </summary>
        public List<RecentActivity> RecentActivities { get; set; } = new();

        /// <summary>
        /// 告警信息
        /// </summary>
        public List<ActiveAlert> ActiveAlerts { get; set; } = new();

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime LastUpdated { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 概览统计
    /// </summary>
    public class OverviewStatistics
    {
        /// <summary>
        /// 总工作流数
        /// </summary>
        public int TotalWorkflows { get; set; }

        /// <summary>
        /// 活跃工作流数
        /// </summary>
        public int ActiveWorkflows { get; set; }

        /// <summary>
        /// 今日完成工作流数
        /// </summary>
        public int CompletedToday { get; set; }

        /// <summary>
        /// 今日失败工作流数
        /// </summary>
        public int FailedToday { get; set; }

        /// <summary>
        /// 平均执行时间（分钟）
        /// </summary>
        public double AverageExecutionTime { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 总用户数
        /// </summary>
        public int TotalUsers { get; set; }

        /// <summary>
        /// 活跃用户数
        /// </summary>
        public int ActiveUsers { get; set; }
    }

    /// <summary>
    /// 活跃工作流信息
    /// </summary>
    public class ActiveWorkflowInfo
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 工作流名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        public WorkflowStatus Status { get; set; }

        /// <summary>
        /// 进度百分比
        /// </summary>
        public int Progress { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 预估剩余时间（分钟）
        /// </summary>
        public int? EstimatedRemainingMinutes { get; set; }

        /// <summary>
        /// 当前步骤
        /// </summary>
        public string CurrentStep { get; set; } = string.Empty;

        /// <summary>
        /// 用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 项目ID
        /// </summary>
        public string? ProjectId { get; set; }
    }

    /// <summary>
    /// 性能指标
    /// </summary>
    public class PerformanceMetrics
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        public double CpuUsage { get; set; }

        /// <summary>
        /// 内存使用率
        /// </summary>
        public double MemoryUsage { get; set; }

        /// <summary>
        /// 磁盘使用率
        /// </summary>
        public double DiskUsage { get; set; }

        /// <summary>
        /// 网络吞吐量
        /// </summary>
        public NetworkThroughput NetworkThroughput { get; set; } = new();

        /// <summary>
        /// 响应时间统计
        /// </summary>
        public ResponseTimeStats ResponseTime { get; set; } = new();

        /// <summary>
        /// 并发连接数
        /// </summary>
        public int ConcurrentConnections { get; set; }

        /// <summary>
        /// 队列长度
        /// </summary>
        public int QueueLength { get; set; }
    }

    /// <summary>
    /// 网络吞吐量
    /// </summary>
    public class NetworkThroughput
    {
        /// <summary>
        /// 入站流量（MB/s）
        /// </summary>
        public double InboundMbps { get; set; }

        /// <summary>
        /// 出站流量（MB/s）
        /// </summary>
        public double OutboundMbps { get; set; }
    }

    /// <summary>
    /// 响应时间统计
    /// </summary>
    public class ResponseTimeStats
    {
        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double Average { get; set; }

        /// <summary>
        /// 最小响应时间（毫秒）
        /// </summary>
        public double Min { get; set; }

        /// <summary>
        /// 最大响应时间（毫秒）
        /// </summary>
        public double Max { get; set; }

        /// <summary>
        /// 95百分位响应时间（毫秒）
        /// </summary>
        public double P95 { get; set; }

        /// <summary>
        /// 99百分位响应时间（毫秒）
        /// </summary>
        public double P99 { get; set; }
    }

    /// <summary>
    /// 系统状态
    /// </summary>
    public class SystemStatus
    {
        /// <summary>
        /// 整体健康状态
        /// </summary>
        public HealthStatus OverallHealth { get; set; }

        /// <summary>
        /// 服务状态列表
        /// </summary>
        public List<ServiceStatus> Services { get; set; } = new();

        /// <summary>
        /// 数据库状态
        /// </summary>
        public DatabaseStatus Database { get; set; } = new();

        /// <summary>
        /// 外部依赖状态
        /// </summary>
        public List<ExternalDependencyStatus> ExternalDependencies { get; set; } = new();

        /// <summary>
        /// 系统启动时间
        /// </summary>
        public DateTime SystemStartTime { get; set; }

        /// <summary>
        /// 运行时间
        /// </summary>
        public TimeSpan Uptime => DateTime.Now - SystemStartTime;
    }

    /// <summary>
    /// 健康状态枚举
    /// </summary>
    public enum HealthStatus
    {
        Healthy,
        Warning,
        Critical,
        Unknown
    }

    /// <summary>
    /// 服务状态
    /// </summary>
    public class ServiceStatus
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus Status { get; set; }

        /// <summary>
        /// 状态消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 最后检查时间
        /// </summary>
        public DateTime LastChecked { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public double ResponseTimeMs { get; set; }
    }

    /// <summary>
    /// 数据库状态
    /// </summary>
    public class DatabaseStatus
    {
        /// <summary>
        /// 连接状态
        /// </summary>
        public HealthStatus ConnectionStatus { get; set; }

        /// <summary>
        /// 连接池大小
        /// </summary>
        public int ConnectionPoolSize { get; set; }

        /// <summary>
        /// 活跃连接数
        /// </summary>
        public int ActiveConnections { get; set; }

        /// <summary>
        /// 查询响应时间（毫秒）
        /// </summary>
        public double QueryResponseTimeMs { get; set; }

        /// <summary>
        /// 数据库大小（MB）
        /// </summary>
        public long DatabaseSizeMB { get; set; }
    }

    /// <summary>
    /// 外部依赖状态
    /// </summary>
    public class ExternalDependencyStatus
    {
        /// <summary>
        /// 依赖名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 依赖类型
        /// </summary>
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        public HealthStatus Status { get; set; }

        /// <summary>
        /// 端点URL
        /// </summary>
        public string? Endpoint { get; set; }

        /// <summary>
        /// 最后检查时间
        /// </summary>
        public DateTime LastChecked { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public double ResponseTimeMs { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 最近活动
    /// </summary>
    public class RecentActivity
    {
        /// <summary>
        /// 活动ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 活动类型
        /// </summary>
        public ActivityType Type { get; set; }

        /// <summary>
        /// 活动描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 工作流ID
        /// </summary>
        public string? WorkflowId { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;

        /// <summary>
        /// 严重程度
        /// </summary>
        public ActivitySeverity Severity { get; set; } = ActivitySeverity.Info;

        /// <summary>
        /// 额外数据
        /// </summary>
        public Dictionary<string, object> Data { get; set; } = new();
    }

    /// <summary>
    /// 活动类型枚举
    /// </summary>
    public enum ActivityType
    {
        WorkflowStarted,
        WorkflowCompleted,
        WorkflowFailed,
        UserLogin,
        UserLogout,
        SystemError,
        ConfigurationChanged,
        AlertTriggered,
        BackupCompleted,
        MaintenanceStarted,
        MaintenanceCompleted
    }

    /// <summary>
    /// 活动严重程度枚举
    /// </summary>
    public enum ActivitySeverity
    {
        Info,
        Warning,
        Error,
        Critical
    }

    /// <summary>
    /// 活跃告警
    /// </summary>
    public class ActiveAlert
    {
        /// <summary>
        /// 告警ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 告警名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 告警级别
        /// </summary>
        public AlertLevel Level { get; set; }

        /// <summary>
        /// 告警消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 触发时间
        /// </summary>
        public DateTime TriggeredAt { get; set; }

        /// <summary>
        /// 来源
        /// </summary>
        public string Source { get; set; } = string.Empty;

        /// <summary>
        /// 是否已确认
        /// </summary>
        public bool IsAcknowledged { get; set; } = false;

        /// <summary>
        /// 确认者
        /// </summary>
        public string? AcknowledgedBy { get; set; }

        /// <summary>
        /// 确认时间
        /// </summary>
        public DateTime? AcknowledgedAt { get; set; }
    }

    /// <summary>
    /// 告警级别枚举
    /// </summary>
    public enum AlertLevel
    {
        Info,
        Warning,
        Error,
        Critical
    }


}
